package son.nt.act2;

import son.nt.R;
import son.nt.common.Contains;
import son.nt.util.GoogleAnaTool;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * @author sonnt
 *
 */

public class WebviewJavaScriptActivity extends Activity{
	public static final String TAG = "WebviewJavaScriptActivity";
	private WebView mWebView = null;
	private ProgressDialog mProgressDialog = null;
	private Context mContext = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act2_webview_javascript);
		mContext = this;
		GoogleAnaTool.track("/WebviewJavaScriptActivity");
		mWebView = (WebView) findViewById(R.id.webviewJava);
		mWebView.setWebViewClient(new Callback());
		mWebView.getSettings().setBuiltInZoomControls(true);
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.addJavascriptInterface(new AndroidBridge(), Contains.ANDROID_BRIDGE);
	}
	public void onClick(View v){
		switch (v.getId()) {
		case R.id.btnWebHome:
			mWebView.loadUrl(Contains.URL_VNEXPRESS);
			break;
		case R.id.btnWebLocal:
			String url = String.format("file:///android_asset/hello.html");
			mWebView.loadUrl(url);
		default:
			break;
		}
	}
	
	private class AndroidBridge{
		public void searchLyric(String mTitle, String mArtist){
			Log.v(TAG, "searchLyric : title: " + mTitle + " ;artist:" + mArtist);
		}
	}
	private class Callback extends WebViewClient{
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			// TODO Auto-generated method stub
			return super.shouldOverrideUrlLoading(view, url);
		}
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			// TODO Auto-generated method stub
			super.onPageStarted(view, url, favicon);
			indicationStart("waiting...");
		}
		@Override
		public void onPageFinished(WebView view, String url) {
			// TODO Auto-generated method stub
			super.onPageFinished(view, url);
			indicationStop();
		}
		
		@Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl) {
			// TODO Auto-generated method stub
			super.onReceivedError(view, errorCode, description, failingUrl);
			indicationStop();
		}
	}
	// back key
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		// TODO Auto-generated method stub
		switch (event.getKeyCode()) {
		case KeyEvent.KEYCODE_BACK:
			if(event.getAction() == KeyEvent.ACTION_DOWN){
				if(mWebView.canGoBack()){
					mWebView.goBack();
				}
			}
			break;

		default:
			
			break;
		}
		return super.dispatchKeyEvent(event);
	}
 
	private void indicationStart(String msg){
		if(mProgressDialog == null){
			mProgressDialog = new ProgressDialog(WebviewJavaScriptActivity.this);
			mProgressDialog.setMessage(msg);
			mProgressDialog.setTitle("WebView");
			mProgressDialog.show();
		}
	}
	private void indicationStop(){
		if(mProgressDialog!= null){
			if(mProgressDialog.isShowing()){
				mProgressDialog.dismiss();
				mProgressDialog = null;
			}
		}
	}
}
